package com.isoftstone.bank.controller.base;

import com.isoftstone.util.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * 描述：图片上传裁剪
 * @author：guoX
 * @date: 2016/7/17 22:48
 *
 */

@Controller
public class ImgCropController extends BaseController {

    @RequestMapping(value="/imgcrop")
    public ModelAndView goUploadExcel()throws Exception{
        ModelAndView mv = this.getModelAndView();
        mv.setViewName("crop/index");
        return mv;
    }

    /**
     * 图片上传
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/imgUpload")
    public ModelAndView imgUpload()throws Exception{
        ModelAndView mv = this.getModelAndView();
        PageData pd = this.getPageData();
        mv.addObject("filePath",pd.getString("filePath"));
        mv.addObject("fileName",pd.getString("fileName"));
        mv.setViewName("crop/cropper");
        return mv;
    }


    /**
     * 图片裁剪
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/ImgCropServlet")
    @ResponseBody
    public Map<String, String> ImgCropServlet(HttpServletRequest request)throws Exception{
        PageData pd = this.getPageData();
        Map<String, String> map = new HashMap<String, String>();
        System.out.println("x: " + request.getParameter("dataX") + "," + request.getParameter("dataY") + "," + request.getParameter("dataWidth") + "," + request.getParameter("dataHeight"));

        // 用户经过剪辑后的图片的大小
        Integer x = Integer.parseInt(request.getParameter("dataX"));
        Integer y = Integer.parseInt(request.getParameter("dataY"));
        Integer w = Integer.parseInt(request.getParameter("dataWidth"));
        Integer h = Integer.parseInt(request.getParameter("dataHeight"));

        //获取原显示图片路径
        String oldImgPath = request.getParameter("oldImgPath");
        //图片后缀
        String fileName = request.getParameter("fileName");
        //WEB应用程序根路径
        String webAppPath = PathUtil.getClasspath();

        /**图片名称:以当前日期*/
        //组装图片真实名称
        String newName = DateUtil.getSdfTimesS();
        // 图片后缀
        String imgRoot = oldImgPath.substring(oldImgPath.lastIndexOf("."));
        // 新地址
        String newPath = PathUtil.getClasspath()+ Const.BANNER + newName + imgRoot;
        //之前上传的图片路径
        webAppPath += oldImgPath;
        System.out.println("原图片路径: " + webAppPath + ",新图片路径: " + webAppPath);
        //进行剪切图片操作
        ImageCut.abscut(webAppPath, webAppPath, x,y,w, h);
        File f = new File(webAppPath);
        if(f.exists()){
            System.out.println("剪切图片大小: "+w+"*"+h+"图片成功!");
            // 上传成功删除原来的图片
           // FileUtil.delFile(webAppPath);
        }
        map.put("msg", Const.SUCCESS);
        return map;
    }
}
